package com.cn.wanxi.servlet.message;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.MessageModel;
import com.cn.wanxi.model.OrdbyModel;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.service.IMessageService;
import com.cn.wanxi.service.impl.MessageServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/message/findAll")
public class MessageFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //2.接收前台传数据过来

        String pageNum=req.getParameter("pageNum");
        String pageSize=req.getParameter("pageSize");
        String name=req.getParameter("name");
        String phone=req.getParameter("phone");
        String mailbox=req.getParameter("mailbox");
        String project=req.getParameter("project");
        String message=req.getParameter("message");
        String startdate=req.getParameter("startdate");
        String enddate=req.getParameter("enddate");
        String feedback=req.getParameter("feedback");
        String ordbydate=req.getParameter("ordbydate");

        //3.封装到实体类UserModel
        OrdbyModel ordbyModel=new OrdbyModel();
        ordbyModel.setOrdbydate(ordbydate);
        System.out.println(ordbydate);
        PageModel pageModel=new PageModel();
        pageModel.setPageNum(pageNum==null || "".equals(pageNum)? 1 :Integer.parseInt(pageNum));
        pageModel.setPageSize(pageSize==null || "".equals(pageSize) ? 10:Integer.parseInt(pageSize));

        //3.封装到实体类UserModel
        MessageModel messageModel=new MessageModel();
        messageModel.setPageModel(pageModel);
        messageModel.setName(name);
        messageModel.setPhone(phone);
        messageModel.setMailbox(mailbox);
        messageModel.setProject(project);
        messageModel.setMessage(message);
        messageModel.setStartdate(startdate);
        messageModel.setEnddate(enddate);
        messageModel.setFeedback(feedback);
        messageModel.setOrdbyModel(ordbyModel);


        //4.服务逻辑层
        IMessageService iMessageService=new MessageServiceImpl();
        //5.调用得到页面所需要的数据

        List<MessageModel> list =iMessageService.findAll(messageModel);
        int number=iMessageService.getCount(messageModel);

        //7.将数据转发到页面
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("number",number);
        resp.getWriter().println(jsonObject);
    }
}
